package zl.core;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

class SplitTools {

    /**
     * 拆分图片识别结果
     */
    void getImageResult(JSONObject res){
        //获取结果的Json对象
        System.out.println("******识别结果******\n");
        JSONArray result = res.getJSONArray("words_result");

        int count = 0;
        for(int i = 0; i < result.length(); i++){
            JSONObject obj = (JSONObject)result.get(i);
            count ++;
            System.out.println(obj.getString("words"));
            //System.out.println("第" + count + "条结果：" + obj.getString("words"));
        }
    }

    /**
     * 拆分银行卡格式
     */
    void getBankCard(JSONObject res){

        //获取识别结果
        System.out.println("******识别结果******\n");
        JSONObject obj = res.getJSONObject("result");

        //获取银行卡信息
        System.out.print("银行卡类别：");
        String name = obj.getString("bank_name");
        System.out.println(name);

        //获取银行卡号
        System.out.print("银行卡号：");
        String number = obj.getString("bank_card_number");
        System.out.println(number);
    }

    /**
     *拆分身份证格式
     */
    void getIDCard(JSONObject res){

        //获取json数据
        System.out.println("*****识别结果*****\n");
        JSONObject obj = res.getJSONObject("words_result");

        //获取姓名
        System.out.print("姓   名：");
        JSONObject name = obj.getJSONObject("姓名");
        String nameWords = name.getString("words");
        System.out.println(nameWords);

        //获取性别
        System.out.print("性   别：");
        JSONObject sex = obj.getJSONObject("性别");
        String sexWorlds = sex.getString("words");
        System.out.println(sexWorlds);

        //获取民族
        System.out.print("民   族：");
        JSONObject mz = obj.getJSONObject("民族");
        String mzWords = mz.getString("words");
        System.out.println(mzWords);

        //获取出生年月
        System.out.print("出生年月：");
        JSONObject age = obj.getJSONObject("出生");
        String ageWords = age.getString("words");
        System.out.println(ageWords);

        //获取公民身份号码
        System.out.print("身份号码：");
        JSONObject idCard = obj.getJSONObject("公民身份号码");
        String idCardWords =idCard.getString("words");
        System.out.println(idCardWords);

        //获取住址
        System.out.print("家庭住址：");
        JSONObject address = obj.getJSONObject("住址");
        String addressWords = address.getString("words");
        System.out.println(addressWords);
    }

    /**
     * 拆分银行卡格式
     */
    void getUrlImage(JSONObject res){
        System.out.println("******识别结果******\n");
        JSONArray urlWorld = res.getJSONArray("words_result");

        int count = 0;
        for(int i = 0; i < urlWorld.length(); i++){
            JSONObject json =(JSONObject)urlWorld.get(i);
            count++;
            System.out.println("Result" + count + ":" + json.getString("words"));
        }
    }


/**
*  获取Excel
* */
    public void getExcel(AipOcr client, JSONObject res) {
//异步接口
// 为使用接口的返回值restableRecognitionAsyncjson
        String request_id = res.getJSONArray("result").getJSONObject(0).getString("request_id");
        System.out.println("request_id => " + request_id);
// 获取结果json
        System.out.println(client .getTableRecognitionJsonResult(request_id));
// 获取结果excel
        System.out.println(client .getTableRecognitionExcelResult(request_id));


    }

    public void sample(AipOcr client)
    {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("result_type", "json");

        String requestId = "23454320-23255";

        // 表格识别结果
        JSONObject res = client.tableResultGet(requestId, options);
        System.out.println(res.toString(2));

    }
}
